Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Sync implements Runnable{
- private Thread monitor;
- public Sync setMonitor(Thread mon){
- monitor = mon;
- return this;
- }
- public void run() {
- try {
- System.out.println(Thread.currentThread().getName() + ": start");
- if (monitor == null)
- Thread.sleep(1000);
- else
- monitor.join(); //ожидание выполнения потока monitor
- System.out.println(Thread.currentThread().getName() + ": end");
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- public class TestJoinMethod{
- public static void main(String[] args) throws Exception{
- Thread ta = new Thread(new Sync(), "threadA");
- Thread tb = new Thread(new Sync().setMonitor(ta), "threadB");
- ta.start();
- tb.start();
- try {
- tb.join(); //ожидание выполнения потока tb
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println(Thread.currentThread().getName() + ": end");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement